package com.awanp3.nimbus.base.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.awanp3.nimbus.common.idempotent.annotation.RepeatSubmit;
import com.awanp3.nimbus.common.log.annotation.Log;
import com.awanp3.nimbus.common.web.core.BaseController;
import com.awanp3.nimbus.common.mybatis.core.page.PageQuery;
import com.awanp3.nimbus.common.core.domain.R;
import com.awanp3.nimbus.common.core.validate.AddGroup;
import com.awanp3.nimbus.common.core.validate.EditGroup;
import com.awanp3.nimbus.common.log.enums.BusinessType;
import com.awanp3.nimbus.common.excel.utils.ExcelUtil;
import com.awanp3.nimbus.base.domain.vo.BaseMaterialVo;
import com.awanp3.nimbus.base.domain.bo.BaseMaterialBo;
import com.awanp3.nimbus.base.service.IBaseMaterialService;
import com.awanp3.nimbus.common.mybatis.core.page.TableDataInfo;

/**
 * 物料
 * 前端访问路由地址为:/base/material
 *
 * @author 20014170
 * @date 2025-09-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/material")
public class BaseMaterialController extends BaseController {

    private final IBaseMaterialService baseMaterialService;

    /**
     * 查询物料分页列表
     */
    @SaCheckPermission("base:material:list")
    @GetMapping("/page")
    public TableDataInfo<BaseMaterialVo> page(BaseMaterialBo bo, PageQuery pageQuery) {
        return baseMaterialService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询物料列表
     */
    @SaCheckPermission("base:material:list")
    @GetMapping("/list")
    public R<List<BaseMaterialVo>> list(BaseMaterialBo bo) {
        List<BaseMaterialVo> list = baseMaterialService.queryList(bo);
        return R.ok(list);
    }


    /**
     * 导出物料列表
     */
    @SaCheckPermission("base:material:export")
    @Log(title = "物料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseMaterialBo bo, HttpServletResponse response) {
        List<BaseMaterialVo> list = baseMaterialService.queryList(bo);
        ExcelUtil.exportExcel(list, "物料", BaseMaterialVo.class, response);
    }

    /**
     * 获取物料详细信息
     *
     * @param materialId 主键
     */
    @SaCheckPermission("base:material:query")
    @GetMapping("/{materialId}")
    public R<BaseMaterialVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("materialId") Long materialId) {
        return R.ok(baseMaterialService.queryById(materialId));
    }

    /**
     * 新增物料
     */
    @SaCheckPermission("base:material:add")
    @Log(title = "物料", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseMaterialBo bo) {
        return toAjax(baseMaterialService.insertByBo(bo));
    }

    /**
     * 修改物料
     */
    @SaCheckPermission("base:material:edit")
    @Log(title = "物料", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseMaterialBo bo) {
        return toAjax(baseMaterialService.updateByBo(bo));
    }

    /**
     * 删除物料
     *
     * @param materialIds 主键串
     */
    @SaCheckPermission("base:material:remove")
    @Log(title = "物料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("materialIds") Long[] materialIds) {
        return toAjax(baseMaterialService.deleteWithValidByIds(List.of(materialIds), true));
    }
}
